package org.mpremote.android.framework.xml;

import android.util.Log;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.ConverterRegistry;
import com.thoughtworks.xstream.core.JVM;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.mapper.AnnotationMapper;

import java.io.InputStream;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 30/09/2010
 * Time: 23:13:59
 */
@SuppressWarnings({"unchecked"})
public class MPXmlParser {

    private static XStream xStream;

    static {
        HierarchicalStreamDriver driver = new XppDriver();
        xStream = new XStream(driver);

        JVM jvm = new JVM();
        AnnotationMapper annotationMapper = new AnnotationMapper(xStream.getMapper(), (ConverterRegistry) xStream.getConverterLookup(), xStream.getClassLoader(), xStream.getReflectionProvider(), jvm);
        xStream = new XStream(xStream.getReflectionProvider(), driver, xStream.getClassLoader(), annotationMapper);
        xStream.processAnnotations(new Class[]{Artist.class, Artists.class, Album.class, Albums.class, ArtistAlbums.class});
    }

    public static <T> T parseXML(InputStream inputStream) {
        Log.i("XMLParser", "Parsing XML");
        return (T) xStream.fromXML(inputStream);
    }
}
